package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:34
 */
public class DeleteBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Borrow Book*************");
        System.out.println("请输入您所要删除图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        int index = -1;
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有查找到该图书！！！");
        }else {
            for (int i = index; i < currentNum-1; i++) {
                Book book = bookList.getBook(i+1);
                bookList.setBooks(book,i);
            }
            bookList.setBooks(null,currentNum);
            bookList.setBookNumber(currentNum-1);
            System.out.println("成功删除该图书！！！");
        }
    }
}
